
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:reabampos/src/reabam/operation/bean/check_app_update/Request_check_app_update.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';
import 'package:xsdk_flutter_plugin/xsdk_flutter_plugin.dart';

class UpdateAPPStatePage extends XPageState{

  BeanUpdateAppVersionInfo info;

  UpdateAPPStatePage(this.info);

  bool isStartDownload=false;
  double downloadProgress=0;

  @override
  Widget getBodyWidget(BuildContext context) {

    dynamic updateType = info.updateType;
    String? updateUrl = info.downloadUrl;

    return XWidgetUtils.getLayoutContainer(
        alignment:Alignment.center,
        child: XWidgetUtils.getLayoutColumn(
            mainAxisAlignment:MainAxisAlignment.center,
            crossAxisAlignment:CrossAxisAlignment.center,
            mainAxisSize:MainAxisSize.min,
            children: [
              XWidgetUtils.getLayoutContainer(
                  width: 320,
                  height: 418.5,
                  child: XWidgetUtils.getLayoutStack(
                      alignment:AlignmentDirectional.topCenter,
                      children: [
                        XWidgetUtils.getLayoutPositioned(
                          bottom: 0,
                            left: 0,
                            child: XWidgetUtils.getXLayoutRoundedRect(
                                width: 320,
                                height: 400,
                                child:XWidgetUtils.getLayoutContainer()
                            ),
                        ),
                        XImageUtils.getImageWidgetWithAsset(
                            'bg_update_app.png',
                            width: 320,
                            height: 176
                        ),
                        XWidgetUtils.getLayoutContainer(
                          margin: XWidgetUtils.getEdgeInsets(0, 88, 0, 0),
                          child: XWidgetUtils.getLayoutColumn(
                              crossAxisAlignment:CrossAxisAlignment.center,
                              children: [
                                XWidgetUtils.getWidgetText(
                                    '发现新版本',
                                    style: XStyleUtils.getTextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 18,
                                      color: Colors.black,
                                    ),
                                    padding: XWidgetUtils.getEdgeInsets(0, 0, 0, 24)
                                ),
                                XWidgetUtils.getWidgetText(
                                    updateType==0?'检测有最新版本.':'检查有重要版本升级.',
                                    style: XStyleUtils.textStyle_999_14(),
                                    padding: XWidgetUtils.getEdgeInsets(0, 0, 0, 0)
                                ),
                                XWidgetUtils.getWidgetText(
                                    '版本号:${info.version}',
                                    style: XStyleUtils.textStyle_999_14(),
                                    padding: XWidgetUtils.getEdgeInsets(0, 5, 0, 0)
                                ),
                                XWidgetUtils.getLayoutContainer(
                                  child: XStringUtils.isEmpty(info.description)?null:XWidgetUtils.getWidgetText(
                                      '${info.description}',
                                      style: XStyleUtils.textStyle_999_14(),
                                      padding: XWidgetUtils.getEdgeInsets(36, 5, 36, 0),
                                  ),
                                ),
                                XWidgetUtils.getLayoutSpacer(),
                                XWidgetUtils.getWidgetGestureDetector(
                                    child: XWidgetUtils.getXLayoutRoundedRect(
                                      margin: XWidgetUtils.getEdgeInsets(0, 0, 0, 25),
                                      alignment: Alignment.center,
                                      backgroundColor: isStartDownload?XColorUtils.color999999:XColorUtils.appMainColor,
                                      width: 117.5,
                                      radius: 16,
                                      height: 33,
                                      child: XWidgetUtils.getWidgetText(
                                        isStartDownload?'进度:${XNumberUtils.d2int(downloadProgress*100)}%':'立即升级',
                                        style: XStyleUtils.textStyle_fff_16(fontWeight: FontWeight.bold),
                                      ),
                                    ),
                                    onTap: (){

                                      if(XFlutterUtils.isiOS()){

                                        if(RBConfigs.versionType==VersionType.baodaoVersion){
                                          //包道
                                          XFlutterUtils.openUri('uri');
                                          toast('打开包道app store url');
                                        }else{
                                          XFlutterUtils.openUri('https://apps.apple.com/cn/app/%E7%9D%BF%E6%9C%AC%E4%BA%91hd/id6469494616');
                                        }

                                      }else{
                                        //android
                                        if(isStartDownload){
                                          toast('正在下载中');
                                          return;
                                        }

                                        showLoading();

                                        String saveFileName='update.apk';

                                        XLogUtils.printLog('---appDocumentsDirectoryPath=${XTempData.appDocumentsDirectoryPath}/$saveFileName');

                                        //
                                        XHttpUtils.download(
                                          url: updateUrl??'',
                                          savePath: '${XTempData.appDocumentsDirectoryPath}/$saveFileName',
                                          callback: (bool isFinish,double progress){

                                            if(!isFinish){
                                              setState(() {
                                                isStartDownload=true;
                                                downloadProgress=progress;
                                              });
                                            }else{
                                              isStartDownload=false;
                                              hideLoading();
                                              XLogUtils.printLog('---download,isFinish=$isFinish,progress=$progress');
                                              XFileUtils.openFile('${XTempData.appDocumentsDirectoryPath}/$saveFileName');
                                            }
                                          },
                                        );
                                      }

                                    }
                                )

                              ]
                          )
                        ),
                      ]
                  )
              ),
              XWidgetUtils.getLayoutContainer(
                margin: updateType==0?XWidgetUtils.getEdgeInsets(0, 24.5, 0, 0):null,
                child: updateType==0?XWidgetUtils.getWidgetGestureDetector(
                    child: XImageUtils.getImageWidgetWithAsset(
                        'del_3.png',
                        width: 27,
                        height: 26.5
                    ),
                    onTap: (){

                      if(isStartDownload){
                        toast('正在下载中');
                        return;
                      }

                      finishPage();
                    }
                ):null,
              ),
            ]
        )
    );

  }

  @override
  Color getXPageBackgroundColor() {
    return const Color(0x80000000);
  }

  @override
  bool getCanHardwarePressBack() {
    return false;
  }

  @override
  void onStart() {
    //申请安装权限
    if(XFlutterUtils.isAndroid()){
      XFlutterPluginUtils.checkInstallPermission();
    }else{
      XPermissionUtils.requestPermission(Permission.requestInstallPackages);
    }

  }

  @override
  Future<void> updateData({int pageIndex = 1}) async {
  }




}